package interview;

public class interview_08_10 {
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        int m=image.length;
        int n=image[0].length;
        boolean[][] flag=new boolean[m][n];
        int oldcolor=image[sr][sc];
        color(image,sr,sc,newColor,flag,oldcolor);
        return image;
    }
    public void color(int[][] image,int i,int j,int color,boolean[][] flag,int oldcolor){
        if(i>=0&&i<image.length&&j>=0&&j<image[0].length&&!flag[i][j]&&image[i][j]==oldcolor){
            flag[i][j]=true;
            image[i][j]=color;
            color(image, i-1, j, color, flag, oldcolor);
            color(image, i, j-1, color, flag, oldcolor);
            color(image, i+1, j, color, flag, oldcolor);
            color(image, i, j+1, color, flag, oldcolor);
        }
    }
}
